package com.kgc.scd.controller;


import com.kgc.scd.annotation.RequestPermission;
import com.kgc.scd.annotation.RequestUser;
import com.kgc.scd.bean.Coupon;
import com.kgc.scd.bean.CouponInn;
import com.kgc.scd.bean.CouponUser;
import com.kgc.scd.contant.CommonConstant;
import com.kgc.scd.service.CouponService;
import com.kgc.scd.util.PageSupport;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import com.kgc.scd.util.UserAnno;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created On : 2022/6/13.
 * <p>
 * Author     : nainai
 * <p>
 * Description: 优惠券操作入口
 */
@Slf4j
@RestController
@Api(tags = "14、优惠券操作入口")
public class CouponController {

    @Autowired(required = false)
    private CouponService couponService;

    /**
     * @param : [java.lang.Integer]
     * @return : com.kgc.scd.util.RequestResult<java.util.List<com.kgc.scd.bean.Coupon>>
     * @author : nainai
     * @date : 2022/6/13
     * @description : 查询商品种类可用优惠券
     */
    @GetMapping("/getCoupons")
    @ApiOperation(value = "查询商城中的优惠券", notes = "根据商品分类查询可用优惠券（不传cid时返回所有优惠券），输入0为查通用券，根据分类查询结果包含全品类通用券，支持统一返回")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cid", value = "商品分类编号，0为通用券", required = false),
            @ApiImplicitParam(name = "pageNo", value = "页码，默认为第一页", required = false, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页面容量，默认为显示3条", required = false, defaultValue = "3")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "SUCCESS"),
            @ApiResponse(code = 500, message = "FAIL")
    })
    public RequestResult<PageSupport<Coupon>> getCoupons(@RequestParam(required = false) Integer cid, @RequestParam(required = false, defaultValue = "1") Integer pageNo, @RequestParam(required = false, defaultValue = "3") Integer pageSize) {
        log.info("web-根据商品分类：{}，开始查询该分类可用优惠券", cid);

        PageSupport<Coupon> coupons = couponService.queryCoupons(cid, pageNo, pageSize);

        if (coupons.getPageData() == null || coupons.getPageData().isEmpty()) {
            return ResultBuildUtil.fail(coupons);
        }
        return ResultBuildUtil.success(coupons);
    }

    /**
     * @param : [com.kgc.scd.util.UserAnno]
     * @return : com.kgc.scd.util.RequestResult<java.util.List<com.kgc.scd.bean.Coupon>>
     * @author : nainai
     * @date : 2022/6/13
     * @description : 查询用户已拥有的优惠券
     */
    @RequestPermission()
    @PostMapping("/getUserCoupons")
    @ApiOperation(value = "查询用户已拥有的优惠券", notes = "根据用户编号，查询用户拥有的优惠券，校验用户是否登录，支持统一返回")
    @ApiResponses({
            @ApiResponse(code = 200, message = "SUCCESS"),
            @ApiResponse(code = 500, message = "FAIL")
    })
    public RequestResult<List<CouponUser>> getUserCoupons(@RequestUser UserAnno userAnno) {
        log.info("web-用户：{}，查询已拥有优惠券", userAnno);
        List<CouponUser> couponUsers = couponService.queryUserCoupons(userAnno);

        // 判断用户是否没有优惠券
        if (couponUsers.isEmpty()) {
            CouponUser couponUser = CouponUser.builder()
                    .couName("优惠券为空")
                    .build();
            return ResultBuildUtil.fail(Arrays.asList(couponUser));
        }

        return ResultBuildUtil.success(couponUsers);
    }

    /**
     * @param : [java.util.List<com.kgc.scd.bean.Coupon>]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @author : nainai
     * @date : 2022/6/14
     * @description : 添加优惠券
     */
    @PostMapping("/addCoupons")
    @ApiOperation(value = "添加优惠券", notes = "支持批量添加，如果优惠券名称重复，不会进行添加，支持统一返回")
    @ApiResponses({
            @ApiResponse(code = 200, message = "SUCCESS"),
            @ApiResponse(code = 504, message = "添加优惠券失败")
    })
    public RequestResult<String> addCoupons(@RequestBody List<Coupon> couponList) {
        log.info("web-添加优惠券，优惠券详情：{}", couponList);

        return couponService.insertCoupons(couponList) ?
                ResultBuildUtil.success() :
                ResultBuildUtil.fail(CommonConstant.BLMALL_RETURN_ADD_COUPON_FAIL_CODE,  CommonConstant.BLMALL_RETURN_ADD_COUPON_FAIL_MSG);
    }

    /**
     * @param : [java.util.List<java.lang.Integer>]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @author : nainai
     * @date : 2022/6/14
     * @description : 修改优惠券启用状态
     */
    @PostMapping("/modCouponsStatus")
    @ApiOperation(value = "修改优惠券启用状态", notes = "支持批量修改，支持统一返回")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "优惠券编号，支持批量修改"),
            @ApiImplicitParam(name = "status", value = "优惠券状态，1为启用，0为未启用")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "SUCCESS"),
            @ApiResponse(code = 505, message = "修改优惠券状态失败")
    })
    public RequestResult<String> changeCouponsStatus(@RequestParam List<Integer> ids, @RequestParam Integer status) {
        log.info("web-修改优惠券状态：{}", ids);

        return couponService.changeCouponsStatus(ids, status) ?
                ResultBuildUtil.success() :
                ResultBuildUtil.fail(CommonConstant.BLMALL_RETURN_CHANGE_COUPON_STATUS_FAIL_CODE,  CommonConstant.BLMALL_RETURN_CHANGE_COUPON_STATUS_FAIL_MSG);
    }

    /**
     * @param : [com.kgc.scd.util.UserAnno, java.lang.Integer]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @author : nainai
     * @date : 2022/6/14
     * @description : 用户领取优惠券
     */
    @RequestPermission
    @PostMapping("/userGetCoupon")
    @ApiOperation(value = "用户领取优惠券", notes = "支持统一返回，积分不足或优惠券不足，则无法领取，默认领取一张")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coId", value = "优惠券编号"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "SUCCESS"),
            @ApiResponse(code = 500, message = "FAIL")
    })
    public RequestResult<String> userGetCoupon(@RequestUser UserAnno userAnno, @RequestParam Integer coId) {
        log.info("web-用户：{}，领取优惠券：{}", userAnno.getUserId(), coId);

        Map<String, String> map = couponService.userPullCoupon(userAnno, coId);

        return map.get("result").equals("true") ?
                ResultBuildUtil.success("领取优惠券成功！") :
                ResultBuildUtil.fail(map.get("result"));
    }

    /**
     * @author : nainai
     * @date   : 2022/6/15
     * @param  : [com.kgc.scd.util.UserAnno, java.lang.String]
     * @return : java.util.List<com.kgc.scd.bean.Coupon>
     * @description : 查询订单可用优惠券
     */
    @PostMapping("/getOrderCoupons")
    @ApiOperation(value = "查询订单可用优惠券", notes = "支持统一返回")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单号"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "SUCCESS"),
    })
    @RequestPermission
    public RequestResult<List<CouponInn>> getOrderCoupons(@RequestUser UserAnno userAnno, @RequestParam String orderId){
        log.info("web-查询订单可用优惠券，用户：{}，订单号：{}", userAnno, orderId);

        // 查询可用优惠券
        return ResultBuildUtil.success(couponService.queryOrderCoupons(userAnno, orderId));
    }

}
